package com.you.concurrent.thread.state;

import java.util.concurrent.TimeUnit;

public class ThreadStateDemo {

    public static void main(String[] args) {
        //测试线程的 Timed_Waiting 状态
        new Thread(() -> {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(100L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "Timed_Waiting_Thread").start();
        //测试线程的 Waiting 状态
        new Thread(() -> {
            synchronized (ThreadStateDemo.class) {
                while (true) {
                    try {
                        ThreadStateDemo.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "Waiting_Thread").start();

        //测试线程的阻塞状态
        new Thread(new BlockedThreadClass(), "BlockedThreadClass_1").start();
        new Thread(new BlockedThreadClass(), "BlockedThreadClass_2").start();
    }

    static class BlockedThreadClass implements Runnable {
        @Override
        public void run() {
            synchronized (BlockedThreadClass.class) {
                try {
                    TimeUnit.SECONDS.sleep(200L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
